package com.gzk.org.controller;

import com.gzk.basic.exception.BusinessException;
import com.gzk.basic.util.AjaxResult;
import com.gzk.basic.util.PageList;
import com.gzk.org.domain.Shop;
import com.gzk.org.query.ShopQuery;
import com.gzk.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {

    //注入
    @Autowired
    private IShopService shopService;

    @GetMapping("/{id}")
    public Shop getById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    @GetMapping("/list")
    public List<Shop> getAll(){
        return shopService.queryAll();
    }

    @PostMapping("/page")
    public PageList<Shop> pageQuery(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            //判断
            if(shop.getId() == null){
                //增加
                shopService.add(shop);
            }else {
                //修改
                shopService.update(shop);
            }
            //成功
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @PatchMapping("/batch")
    public AjaxResult deleteMany(@RequestBody List<Long> ids){
        try {
            shopService.removeMany(ids);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            //成功
            return AjaxResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }
}
